-- UIGameAchivInfo
-- Created by chengb Sep/12/2015
-- 成就信息界面

UIGameAchivInfo = class("UIGameAchivInfo", function()
    return cc.Layer:create();
end);

function UIGameAchivInfo.create(id)
    return UIGameAchivInfo.new(id);
end

-- 背景框切片数量
local SMALL_BG_COUNT = 4;

-- 大底框
local LARGE_TYPE = "large";

-- 小底框
local SMALL_TYPE = "small";

-- 构造函数
function UIGameAchivInfo:ctor(id)
    self:setName("UIGameAchivInfo");

    self.id = id;
    self.formType = SMALL_TYPE;

    -- 初始化
    self:loadDiffTempByContent();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();

    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            cclog("UIGameAchivInfo 界面析构清理");
        end
    end);
end

-- 适配
function UIGameAchivInfo:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");
end

-- 注册点击事件
function UIGameAchivInfo:registerTouchEvent()
    -- 点击背景关闭界面
    local bgNode = findChildByName(self.node, "bg/bgImg");
    bgNode:setTouchEnabled(true);
    local function onBgClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            closeFormByName("UIGameAchivInfo");
        end
    end
    bgNode:addTouchEventListener(onBgClick);

    local frameNode = findChildByName(self.node, "CT/bg_frame");
    for i = 1, SMALL_BG_COUNT do
        local bgImg = findChildByName(frameNode, "bg"..i);
        bgImg:setTouchEnabled(true);
        bgImg:addTouchEventListener(onBgClick);
    end
end

-- 根据内容加载不同的模板
function UIGameAchivInfo:loadDiffTempByContent()
    local config = GameAchievementM.query(self.id);

    local path = "";
    if config["rule"] == "gather_suit" or
        config["rule"] == "max_career" or
        config["rule"] == "dragon_wish_type" then
        path = "layout/achievement/GameAchivInfo2.csb";
        self.formType = LARGE_TYPE;
    else
        path = "layout/achievement/GameAchivInfo.csb";
        self.formType = SMALL_TYPE;
    end

    local node = cc.CSLoader:createNode(path);
    self:addChild(node);
    self.node = node;
end

-- 重绘
function UIGameAchivInfo:redraw()
    local id = self.id;
    local config = GameAchievementM.query(id);

    -- 名称
    local nameLabel = findChildByName(self.node, "CT/name");

    -- 将阶位转成罗马数字
    local rank = getRomanNumeral(config["rank"]);
    TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_GREEN);
    nameLabel:setString(config["name"] .. " " .. rank);

    -- 描述
    local descLabel = findChildByName(self.node, "CT/desc");
    local desc = GameAchievementM.queryDesc(id);
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    local specialDescLabel = findChildByName(self.node, "CT/special_desc");
    TextStyleM.setTextStyle(specialDescLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);

    if config["rule"] == "gather_suit" then
        -- 是收集套装，需要显示套装信息
        local activeCount = 0;
        local ruleArg = config["rule_arg"];
        local totalCount = #ruleArg["suits"];
        local activeSuits = ME.user.dbase:query("active_suits", {});
        local nameStr = "";
        for i, suitId in pairs(ruleArg["suits"]) do
            local suitName = SuitM.query(suitId, "name");

            -- 统计已收齐的数量
            if table.indexOf(activeSuits, suitId) ~= -1 then
                activeCount = activeCount + 1;
                nameStr = nameStr .. suitName;
            else
                -- 未激活的用灰色显示
                nameStr = nameStr .. "[gray]" .. suitName .. "[-]";
            end

            if i ~= totalCount then
                nameStr = nameStr .. "\n";
            end
        end

        specialDescLabel:setVisible(true);
        specialDescLabel = applyString(specialDescLabel, nameStr);
    elseif config["rule"] == "max_career" then
        -- 是称号升级，需要显示称号信息
        local activeCount = 0;
        local ruleArg = config["rule_arg"];
        local totalCount = #ruleArg["career"];
        local careerRecord = ME.user.dbase:query("max_career_record", {});
        local nameStr = "";
        for i, careerId in pairs(ruleArg["career"]) do
            local careerName = CareerM.getCareerInfo(careerId, "name");

            -- 统计已收齐的数量
            if table.indexOf(careerRecord, careerId) ~= -1 then
                activeCount = activeCount + 1;
                nameStr = nameStr .. careerName;
            else
                -- 未激活的用灰色显示
                nameStr = nameStr .. "[gray]" .. careerName .. "[-]";
            end

            if i ~= totalCount then
                nameStr = nameStr .. "\n";
            end
        end

        specialDescLabel:setVisible(true);
        specialDescLabel = applyString(specialDescLabel, nameStr);
    elseif config["rule"] == "dragon_wish_type" then
        -- 需要显示神龙许愿的种类
        -- 先加上进度
        local curProgress = GameAchievementM.getProgress(id);
        local progress = config["progress"];
        local ruleArg = GameAchievementM.query(id, "rule_arg") or {};
        desc = desc .. "\n" .. string.format(getLocStr("achievement_progress"), curProgress, progress);

        -- 显示愿望
        local ruleArg = config["rule_arg"];
        local totalCount = #ruleArg["wishes"];
        local wishInfo = ME.user.dbase:query("wish_info", {});
        local optionTimes = wishInfo["option_times"] or {};
        local nameStr = "";

        for _, id in pairs(ruleArg["wishes"]) do
            local wishName = DragonWishM.query(id, "options_text")
            -- 已经许过的愿望
            if optionTimes[id] ~= nil then
                nameStr = nameStr .. wishName;
            else
                -- 未激活的用灰色显示
                nameStr = nameStr .. "[gray]" .. wishName .. "[-]";
            end
            nameStr = nameStr .. "\n";
        end

        specialDescLabel:setVisible(true);
        specialDescLabel = applyString(specialDescLabel, nameStr);
    else
        -- 其他成就，加上进度
        specialDescLabel:setVisible(false);
        local curProgress = GameAchievementM.getProgress(id);
        local progress = config["progress"];
        local ruleArg = GameAchievementM.query(id, "rule_arg") or {};
        if ruleArg["minute_to_hour"] == 1 then
            -- 分钟转换成小时
            progress = math.floor(progress/60);
            curProgress = math.floor(curProgress/60);
        end

        desc = desc .. "\n" .. string.format(getLocStr("achievement_progress"), curProgress, progress);
    end

    descLabel = applyString(descLabel, desc);

    -- 积分
    local scoreLabel = findChildByName(self.node, "CT/score");
    TextStyleM.setTextStyle(scoreLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BLUE);
    scoreLabel:setString(string.format(getLocStr("bonus_achievement_score"), config["score"]));

    -- 自适应所有组件
    local bg = findChildByName(self.node, "CT/bg_frame");
    local alignBgType = self.formType == SMALL_TYPE and AlignM.BG_SIZE_SMALL or AlignM.BG_SIZE_NORMAL;
    AlignM.adaptLayout(bg, alignBgType,
        { nameLabel, descLabel, specialDescLabel, scoreLabel });

    -- 根据适配后的界面高度比例，对界面上的角标位置进行修正
    local bg1 = findChildByName(self.node, "CT/bg_left");
    local bg2 = findChildByName(self.node, "CT/bg_right");

    -- 这里要注意一下，原始的模版高度并不是100%，而是85%。。不解
    local fixY, deltaY = math.modf(alignBgType.height * (1 - bg:getScaleY()) / 2);

    if fixY > 0 then
        fixY = fixY + 2;
    else
        fixY = fixY + 3;
    end

    bg1:setPositionY(bg1:getPositionY() - fixY);
    bg2:setPositionY(bg2:getPositionY() - fixY);

end



